#include "scandcmfilethread.h"
#include "../../ImageAcquisitor/ImageAcquisitor/global.h"
#include "importstudymodel.h"
#include "dcmimportplugin_global.h"

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dcfilefo.h"
#include "dcmtk/dcmdata/dcdatset.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmsr/dsrdoc.h"
#include "dcmtk/dcmimgle/dcmimage.h"

ScanDcmFileThread::ScanDcmFileThread(QObject *parent) :
    QThread(parent)
{
}

void ScanDcmFileThread::run()
{
    abort = false;
    foreach (QString file, fileList) {
        if (abort) break;
        StudyRecord *study = 0;
        DcmFileFormat dcmFile;
        OFCondition cond = dcmFile.loadFile(file.toLocal8Bit().data());
        DcmDataset *dset = dcmFile.getDataset();
        if (cond.good() && dset) {
            const char *value = 0;
            QString studyUid, seriesUid, instUid, sopClassUid;
            dset->findAndGetString(DCM_StudyInstanceUID, value);
            studyUid = QString::fromLatin1(value);
            dset->findAndGetString(DCM_SeriesInstanceUID, value);
            seriesUid = QString::fromLatin1(value);
            dset->findAndGetString(DCM_SOPInstanceUID, value);
            instUid = QString::fromLatin1(value);
            dset->findAndGetString(DCM_SOPClassUID, value);
            sopClassUid = QString::fromLatin1(value);

            if (!(studyUid.isEmpty() || seriesUid.isEmpty() ||
                  instUid.isEmpty() || sopClassUid.isEmpty())) {
                study = new StudyRecord;
                study->studyIUid = studyUid;
                dset->findAndGetString(DCM_AccessionNumber, value);
                study->accNumber = QString::fromLatin1(value).remove(QChar(' '));
                dset->findAndGetString(DCM_PatientID, value);
                study->patientId = QString::fromLocal8Bit(value).remove(QChar(' '));
                dset->findAndGetString(DCM_PatientName, value);
                study->patientName = QString::fromLocal8Bit(value);
                dset->findAndGetString(DCM_PatientSex, value);
                study->patientSex = QString::fromLocal8Bit(value).remove(QChar(' '));
                dset->findAndGetString(DCM_PatientBirthDate, value);
                study->patientBirth = QDate::fromString(QString::fromLatin1(value), DICOM_DATE_FORMAT);
                dset->findAndGetString(DCM_PatientAge, value);
                study->patientAge = QString::fromLocal8Bit(value).remove(QChar(' '));
                dset->findAndGetString(DCM_AdmissionID, value);
                study->admissionId = QString::fromLocal8Bit(value);
                dset->findAndGetString(DCM_MedicalAlerts, value);
                study->medicalAlerts = QString::fromLocal8Bit(value);
                dset->findAndGetString(DCM_StudyDate, value);
                study->studyTime.setDate(QDate::fromString(QString::fromLatin1(value), DICOM_DATE_FORMAT));
                dset->findAndGetString(DCM_StudyTime, value);
                study->studyTime.setTime(formatDicomTime(QString::fromLatin1(value)));
                dset->findAndGetString(DCM_StudyDescription, value);
                study->studyDesc = QString::fromLocal8Bit(value);
                dset->findAndGetString(DCM_InstitutionName, value);
                study->institution = QString::fromLocal8Bit(value);
                dset->findAndGetString(DCM_Modality, value);
                study->modality = QString::fromLatin1(value);
                dset->findAndGetString(DCM_BodyPartExamined, value);
                study->bodyPart = QString::fromLocal8Bit(value);

                ImageRecord *image = new ImageRecord;
                image->imageIUid = instUid;
                image->imageCUid = sopClassUid;
                image->seriesIUid = seriesUid;
                image->studyIUid = studyUid;
                image->file = file;
                study->imageList.append(image);

                emit resultReady(study);
            }
        }
    }
}
